{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "def get_file_content(file_path):\n",
    "    \"\"\"读取文件内容并返回其哈希值\"\"\"\n",
    "    with open(file_path, 'rb') as file:\n",
    "        content = file.read()\n",
    "        return hashlib.md5(content).hexdigest()\n",
    "\n",
    "def find_duplicate_files(root_dir):\n",
    "    \"\"\"查找指定目录下内容相同的文件\"\"\"\n",
    "    file_dict = {}\n",
    "    for dirpath, dirnames, filenames in os.walk(root_dir):\n",
    "        for filename in filenames:\n",
    "            file_path = os.path.join(dirpath, filename)\n",
    "            file_content_hash = get_file_content(file_path)\n",
    "            if file_content_hash in file_dict:\n",
    "                file_dict[file_content_hash].append(file_path)\n",
    "            else:\n",
    "                file_dict[file_content_hash] = [file_path]\n",
    "\n",
    "    # 过滤出内容相同的文件\n",
    "    duplicate_files = {hash: paths for hash, paths in file_dict.items() if len(paths) > 1}\n",
    "    return duplicate_files\n",
    "\n",
    "def main():\n",
    "    root_directory = input(\"请输入要搜索的目录路径: \")\n",
    "    duplicate_files = find_duplicate_files(root_directory)\n",
    "    \n",
    "    if duplicate_files:\n",
    "        print(\"找到以下内容相同的文件:\")\n",
    "        for file_hash, paths in duplicate_files.items():\n",
    "            print(f\"哈希值: {file_hash}\")\n",
    "            for path in paths:\n",
    "                print(f\"文件路径: {path}\")\n",
    "            print()\n",
    "    else:\n",
    "        print(\"没有找到内容相同的文件。\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有找到内容相同的文件。\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "def get_file_content(file_path):\n",
    "    \"\"\"读取文件内容并返回其哈希值\"\"\"\n",
    "    with open(file_path, 'rb') as file:\n",
    "        content = file.read()\n",
    "        return hashlib.md5(content).hexdigest()\n",
    "\n",
    "def find_duplicate_files(root_dir):\n",
    "    \"\"\"查找指定目录下内容相同的文件\"\"\"\n",
    "    file_dict = {}\n",
    "    for dirpath, dirnames, filenames in os.walk(root_dir):\n",
    "        for filename in filenames:\n",
    "            file_path = os.path.join(dirpath, filename)\n",
    "            file_content_hash = get_file_content(file_path)\n",
    "            if file_content_hash in file_dict:\n",
    "                file_dict[file_content_hash].append(file_path)\n",
    "            else:\n",
    "                file_dict[file_content_hash] = [file_path]\n",
    "\n",
    "    # 过滤出内容相同的文件\n",
    "    duplicate_files = {hash: paths for hash, paths in file_dict.items() if len(paths) > 1}\n",
    "    return duplicate_files\n",
    "\n",
    "def main():\n",
    "    root_directory = input(\"请输入要搜索的目录路径: \")\n",
    "    duplicate_files = find_duplicate_files(root_directory)\n",
    "    \n",
    "    if duplicate_files:\n",
    "        print(\"找到以下内容相同的文件:\")\n",
    "        for file_hash, paths in duplicate_files.items():\n",
    "            print(f\"哈希值: {file_hash}\")\n",
    "            for path in paths:\n",
    "                print(f\"文件路径: {path}\")\n",
    "            print()\n",
    "    else:\n",
    "        print(\"没有找到内容相同的文件。\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
